package com.wst.system.server.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wst.system.config.exception.GlobalExcetion;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wst.system.server.entity.SysOpenApi;
import com.wst.system.server.entity.SysOpenApp;
import com.wst.system.server.entity.vo.PageSearchVo;
import com.wst.system.server.service.SysOpenApiService;
import com.wst.system.server.service.SysOpenAppService;
import com.wst.system.util.JSONUtil;
import com.wst.system.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wst.system.server.entity.SysOpenPermission;
import com.wst.system.server.mapper.SysOpenPermissionMapper;
import com.wst.system.server.service.SysOpenPermissionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;
import com.wst.system.server.entity.vo.SysOpenPermissionVo;
import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author X.I.O
 * @since 2025-01-16
 */
@Service
@Transactional
public class SysOpenPermissionServiceImpl extends ServiceImpl<SysOpenPermissionMapper, SysOpenPermission> implements SysOpenPermissionService {

    @Autowired
    private SysOpenApiService sysOpenApiService;

    @Autowired
    private SysOpenAppService  sysOpenAppService;

    @Override
    public SysOpenPermission getById(Serializable id){
        return super.getById(id);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }


    @Override
    public boolean updateById(SysOpenPermission sysopenpermission) {
        return super.updateById(sysopenpermission);
    }


    public SysOpenPermission saveSysOpenPermission(SysOpenPermission sysopenpermission) throws GlobalExcetion {
        super.save(sysopenpermission);
        return sysopenpermission;
    }


    public SysOpenPermission updateSysOpenPermissionById(SysOpenPermission sysopenpermission) throws GlobalExcetion{
        super.updateById(sysopenpermission);
        return sysopenpermission;
    }


    public SysOpenPermission saveOrUpdateSysOpenPermission(SysOpenPermissionVo sysopenpermissionVo) throws GlobalExcetion{
        SysOpenPermission sysopenpermission=new SysOpenPermission();
        BeanUtils.copyProperties(sysopenpermissionVo,sysopenpermission);
        SysOpenApi sysOpenApi=sysOpenApiService.getById(sysopenpermissionVo.getApiId());
        if(sysOpenApi==null){
            throw new GlobalExcetion(1,"api接口不存在");
        }
        SysOpenApp sysOpenApp=sysOpenAppService.getById(sysopenpermissionVo.getAppId());
        if(sysOpenApp==null){
            throw new GlobalExcetion(1,"app不存在");
        }
        SysOpenPermission openPermission=this.getSysOpenPermissionByAppIdAndApiId(sysopenpermissionVo.getAppId(),sysopenpermissionVo.getApiId());
        if(sysopenpermissionVo.getId()!=null){
            SysOpenPermission sysopenpermissionEntity=super.baseMapper.selectById(sysopenpermissionVo.getId());
            if(sysopenpermissionEntity==null){
                throw new GlobalExcetion(1,"数据不存在");
            }
            if(openPermission!=null){
                throw new GlobalExcetion(1,"权限已存在");
            }
            super.baseMapper.updateById(sysopenpermission);
        }else{
            if(openPermission!=null){
                throw new GlobalExcetion(1,"权限已存在");
            }
            super.baseMapper.insert(sysopenpermission);
        }
        return sysopenpermission;
    }

    public JSONObject page(PageSearchVo vo) throws GlobalExcetion{
        Map<String,Object> map=new HashMap<>();
        if(vo.getSearch()!=null){
            map.putAll(vo.getSearch());
        }
        Integer page=(vo.getPageNum()-1)*vo.getPageSize();
        List<Map<String,Object>> mapList=this.getBaseMapper().selectPage(map,page, vo.getPageSize());
        Integer count=this.getBaseMapper().selectCount(map);
        return ResultUtil.successInfoTable(count,mapList);
    }


    @Override
    public SysOpenPermission getSysOpenPermissionByAppIdAndApiId(String appId, String apiId) throws GlobalExcetion {
        LambdaQueryWrapper<SysOpenPermission> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysOpenPermission::getAppId,appId);
        lambdaQueryWrapper.eq(SysOpenPermission::getApiId,apiId);
        return this.baseMapper.selectOne(lambdaQueryWrapper);
    }

    @Override
    public List<SysOpenPermission> getSysOpenPermissionListByAppId(String appId) throws GlobalExcetion {
        LambdaQueryWrapper<SysOpenPermission> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysOpenPermission::getAppId,appId);
        return this.baseMapper.selectList(lambdaQueryWrapper);
    }


}